<?php


namespace app\api\controller;


use app\api\model\Score;

class Sign extends BaseController
{
    /**
     * 按上次签到时间算一天
     */
    public function qiandao()
    {
        $user_id = Session('user');
        $user_id = 1;
        $last_sign_times = db('sign')->where('user_id', 1)->field('last_sign_time')->find();
        //数据库时间转化为时间戳
        $last_sign_time = strtotime($last_sign_times['last_sign_time']);
        //现在时间
        $now_time = time();
        //下一天这个时间
        $ints = $now_time + 86400;
        //上一天这个时间
        $int_s = $now_time - 86400;
        //当天已签到
        if ($int_s < $last_sign_time && $last_sign_time < $ints) {
            return json(['code' => 401, 'msg' => '今日您已签到']);
        }
        //昨天未签到
        if ($last_sign_time < $int_s) {
            //签到总天数
            $count = 1;
            $time = date('Y-m-d H:i:s');
            db('sign')->where('user_id', $user_id)->update(['total_day' => $count, 'last_sign_time' => $time]);
            return json(['code' => 401, 'msg' => '签到成功']);
        }
        //今日签到
        if ($int_s < $last_sign_time && $last_sign_time < $now_time) {
            //总签到数
            $num = db('sign')->where('user_id', $user_id)->field('total_day')->find();
            $count = $num + 1;
            //签到增加积分
            $scores = db('score')->where('user_id', $user_id)->field('user_score', 'addscore')->find();
            $score = $scores['user_score'] + $scores['addscore'];
            //点击签到后更新签到时间
            $now_sign_time = date('Y-m-d H:s:i');
            db('score')->where('user_id', $user_id)->update(['user_score' => $score]);
            db('sign')->where('user_id', $user_id)->update(['last_login_time' => $now_sign_time, 'total_day' => $count]);
            return json(['code' => 401, 'msg' => '签到成功']);
        }
    }

    /**
     * 按0点算一天
     *
     * @return array|\think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function dayQian()
    {
        $user_id = Session('user');
        $user_id = 1;
        //上次签到时间
        $last_sign_times = db('sign')->where('user_id', 1)->field('last_sign_time')->find();
        $last_sign_time = strtotime($last_sign_times['last_sign_time']);
        //获取当前时间
        $time = time();
        //获取当天0点的时间戳
        $today = strtotime(date("Y-m-d"), time());
        //获取当天24点的时间戳
        $end = $today + 60 * 60 * 24;
        //获取昨天0点的时间戳
        $yesterday = strtotime('yesterday');
        //今日已签到
        if ($last_sign_time > $today && $last_sign_time < $end) {
            return $this->renderSuccess('今日您已签到');
        }
        //昨日已签到
        if ($last_sign_time < $today && $last_sign_time > $yesterday) {
            $num = db('sign')->where('user_id', $user_id)->field('total_day')->find();
            $count = $num['total_day'] + 1;
            //签到增加积分
            $scores = db('score')->where('user_id', $user_id)->find();

            $score = $scores['user_score'] + $scores['addscore'];
            $now_sign_time = date('Y-m-d H:s:i');
            $data = [
                'last_sign_time' => $now_sign_time,
                'total_day' => $count
            ];
            db('score')->where('user_id', $user_id)->update(['user_score' => $score]);
            db('sign')->where('user_id', $user_id)->update($data);
            return $this->renderSuccess('恭喜您,您已连续签到 ."$count".天');
        }
        //昨日未签到,签到天数归0
        if ($last_sign_time < $yesterday) {
            //签到总天数
            $count = 1;
            $time = date('Y-m-d H:i:s');
            //签到增加积分
            $scores = db('score')->where('user_id', $user_id)->find();
            $score = $scores['user_score'] + $scores['addscore'];
            $now_sign_time = date('Y-m-d H:s:i');
            $data = [
                'last_sign_time' => $time,
                'total_day' => $count
            ];
            db('score')->where('user_id', $user_id)->update(['user_score' => $score]);
            db('sign')->where('user_id', $user_id)->update($data);
            return $this->renderSuccess('恭喜您,您已签到成功');
        }
    }

    /**
     * 签到7天获得签到达人头衔
     */
    public function signMan()
    {
        $user_id = Session('user');
        $res = db('sign')->where('user_id', 1)->field('total_day')->find();
        if ($res['total_day'] >= 7) {
            // return json(['code' => 401, 'msg' => '您已连续签到7日,将获得签到达人头衔']);
            return $this->renderSuccess('您已连续签到7日,将获得签到达人头衔');
        }
    }

    /**
     * 当前用户的积分
     */
    public function myScore()
    {
        $user_id = Session('user');
        $id = 1;
        $score = new Score();
        $score = $score->getScore($id);
        return $this->renderSuccess(compact('score'));
    }
}